package com.longxin.demoapp.base.controller;

import com.longxin.demoapp.R;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.longxin.demoapp.MainController;
import com.longxin.demoapp.base.service.IBaseService;
import com.longxin.demoapp.experiments.experiment1.controller.Experiment1Controller;
import com.longxin.demoapp.experiments.experiment2.controller.Experiment2Controller;
import com.longxin.demoapp.experiments.experiment3.controller.Experiment3Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Long
 * @version 1.0
 * @description:
 * @remarks:
 * @title MyMenu
 * @date 2021/4/6 16:49
 */
public class IBaseController<S extends IBaseService> extends AppCompatActivity {

    protected S service;


    /**
     * 菜单列表
     */
    private static final Map<Integer, Class<?>> menuMap = new HashMap<>();

    static {
        menuMap.put(R.id.main, MainController.class);
        menuMap.put(R.id.exit, IBaseController.class);
        menuMap.put(R.id.experiment_1_menu, Experiment1Controller.class);
        menuMap.put(R.id.experiment_2_menu, Experiment2Controller.class);
        menuMap.put(R.id.experiment_3_menu, Experiment3Controller.class);
    }

    // 菜单的监听方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Class<?> clazz = menuMap.get(item.getItemId());
        if (clazz == null) {
            return false;
        }
        if (clazz == IBaseController.class) {
            finish();
        }
        Toast.makeText(this, "跳转中...", Toast.LENGTH_SHORT).show();
        finish();
        Intent intent = new Intent(this, clazz);
        startActivityForResult(intent, 1);
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_activity, menu);
        return true;
    }

}
